package com.vic.queue_param.exclusive;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.vic.utils.RabbitmqUtils;

/**
 */
public class Producer2 {

    public static void main(String[] args) throws Exception {

        String queueName = "queue_exclusive";

        Connection conn = RabbitmqUtils.getConn();
        Channel channel = conn.createChannel();

        // 排它队列，当声明为排它队列时，durable即使为true，在断开连接依然会删除队列。
        boolean exclusive = true;
        AMQP.Queue.DeclareOk declareOk1 = channel.queueDeclare(queueName, true, exclusive, false, null);

        // 发送消息
        channel.basicPublish("", queueName, null, "Hello".getBytes());

        // close the channel, check if the queue is deleted
        System.out.println("Try to close channel");
        channel.close();
        System.out.println("Channel closed");

        System.out.println("Create a new channel");
        Channel channel2 = conn.createChannel();
        AMQP.Queue.DeclareOk declareOk2 = channel2.queueDeclarePassive(queueName);

        // we can access the exclusive queue from another channel
        System.out.println(declareOk2.getQueue()); // will output "queue_exclusive"
        channel2.basicPublish("", queueName, null, "Hello2".getBytes());
        System.out.println("Message published through the new channel");

//            System.out.println("Try to close Connection");
//            conn.close();
//            System.out.println("Connection closed");

    }

}
